#ifndef PAGESELETIONSHELL_H
#define PAGESELETIONSHELL_H

#include <QObject>
#include "../../XXqtDefine.h"
#include <QLabel>
#include <QAbstractButton>
#include <QLineEdit>

#include <QIntValidator>
#include <QComboBox>

class PageSelectionShell : public QObject
{
    Q_OBJECT
public:
    explicit PageSelectionShell(QObject *parent = nullptr);

    // shell
    void shell(QAbstractButton *first, QAbstractButton *previous, QLabel *page, QAbstractButton *next, QAbstractButton *last, QLineEdit *pageSwitch, QComboBox *availableCountPerPage, QLabel *total);

    // config/setting
    void consetCountPerPage(int count, QList<int> available = {});

    // action
    void reset(int count, int pageIndex = 0);
    void reset(int count, int countPerPage, QList<int> availableCountPerPage, int pageIndex = 0);
    void insertItem(int index);
    void appendItem();
    void removeItemAtIndex(int index);
    void removeItemLast();
    bool isCurrentPage(int index);

    void nextPage();
    void previousPage();

    int getCurrentPageIndex();
    int getPageCount();
    int getCountPerPage();
    int getIndexFromRowInPage(int row);

    int getRowFromIndex(int index);
    int getBeginIndexInPage();
    int getCountInPage();
    bool isIndexAtCurrentPage(int index);

    // event
    XX_KEY(kSigNeedUpdate)
    Q_SIGNAL void sigEvent(QString name, QVariant info);

private:
    QAbstractButton *first();
    QAbstractButton *previous();
    QLabel *page();
    QAbstractButton *next();
    QAbstractButton *last();

    QLineEdit* pageSwitch();
    QComboBox* availableCountPerPage();
    QLabel* itemTotal();

    void setCountPerPageLess(int count, bool &needUpdate);
    void setCountLess(int count, bool &needUpdate);
    void setPageIndexLess(int index, bool &needUpdate);
    void setCount1(int count);
    void setPageIndex1(int index);
    void setAvailableCountPerPageLess(int count, QList<int> available, bool &needUpdate);

    void updateCountTitle();
    void updatePageTitle();
    void updateButtonState();
    void updatePageSwitch();

private slots:
    void button_firstPage_onClicked();
    void button_previousPage_onClicked();
    void button_nextPage_onClicked();
    void button_lastPage_onClicked();

    void lineEdit_pageSwitch_onTextEdited(const QString &text);
    void lineEdit_pageSwitch_onEditingFinished();
    void comboBox_availableCountPerPage_onCurrentIndexChanged(const QString &text);

private:
    QAbstractButton *_button_first;
    QAbstractButton *_button_previous;
    QLabel *_label_page;
    QAbstractButton *_button_next;
    QAbstractButton *_button_last;
    QLineEdit *_lineEdit_pageSwitch;
    QComboBox *_comBobox_availableCountPerPage;
    QLabel *_label_itemTotal;

    int _pageIndex;
    int _count;
    int _countPerPage;
    QList<int> _availableCountPerPage;
    int _pageCount;
};

#endif // BUTTONPAGE_H
